'use strict';
const cashierSDK = {}


const getQueryStr = function getQueryStr() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

  var param = '';
  for (var key in data) {
    param += key + '=' + data[key] + '&';
  }
  param = param.substring(0, param.length - 1);
  return param;
};

const objToStr = function objToStr() {
  var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

  var str = JSON.stringify(obj);
  str = str.replace(/\"/g, "'");
  return encodeURIComponent(str);
};

const CashierHref = {
  development: "http://10.237.157.127:7101/h5/unitaryCashier/index.html#/",
  test: "http://wehoteltest.bestwehotel.com/h5-unitary-cashier/index.html#/",
  prev: "http://mebcenterpoc.bestwehotel.com/h5/h5/h5-unitary-cashier/index.html#/",
  production: "https://mebcenter.bestwehotel.com/h5_Projects/H5Cashier/index.html#/"
};

cashierSDK.setOptions = function (options) {
  var requiredArr = ['env', 'merchantNo', 'merchantOrderNo', 'token', 'subject', 'totalFee', 'callbackUrl'];
  for (var key in options) {
    if (options.hasOwnProperty(key)) {
      if (requiredArr.indexOf(key) !== -1 && !options[key]) {
        return;
      }
      if (key === 'callbackUrl' || key === 'payCancelUrl') options[key] = encodeURIComponent(options[key]);
      if (key === 'paySubOrderList' || key === 'extensions') {
        options[key] = objToStr(options[key]);
      }
    }
  }
  console.log('进来cashierSDK.setOptions')
  window.location.href = CashierHref[options.env] + '?' + getQueryStr(options);
};

export default cashierSDK
